#include <iostream>
using namespace std;

#define MAXN 1005
#define INF 2147483647

int N, M, X;

int map[MAXN][MAXN] = {0};
int rmap[MAXN][MAXN] = {0};

int cost[MAXN];
int rcost[MAXN];
bool used[MAXN];

void dijkstra(int graph[MAXN][MAXN], int s, int min_cost[MAXN]) {
    for (int i = 0; i < MAXN; i++) {
        min_cost[i] = INF;
        used[i] = false;
    }
    min_cost[s] = 0;

    while (true) {
        int v = -1;
        for (int u = 1; u <= N; u++) {
            if (!used[u] && (v == -1 || min_cost[u] < min_cost[v]))
                v = u;
        }

        if (v == -1) break;

        used[v] = true;
        
        for (int i = 1; i <= N; i++) {
            if (graph[v][i] != INF) {
                min_cost[i] = min(min_cost[v] + graph[v][i], min_cost[i]);
            }
        }
    }
}

int main() {
    cin >> N >> M >> X;

    for (int i = 0; i < MAXN; i++) {
        for (int j = 0; j < MAXN; j++) {
            map[i][j] = INF;
            rmap[i][j] = INF;
        }
    }

    for (int i = 0; i < M; i++) {
        int a, b, t;
        cin >> a >> b >> t;
        map[a][b] = t;
        rmap[b][a] = t;
    }

    dijkstra(map, X, cost);
    dijkstra(rmap, X, rcost);

    int c = 0;
    for (int i = 1; i <= N; i++) {
        if (c < cost[i] + rcost[i]) {
            c = cost[i] + rcost[i];
        }
    }

    cout << c << endl;

    return 0;
}
